package com.wayin.lift.service.impl;

import com.wayin.common.utils.R;
import com.wayin.lift.dao.PersonnelDao;
import com.wayin.lift.entity.PersonnelEntity;
import com.wayin.lift.entityVO.ClockInVO;
import com.wayin.lift.entityVO.ResultVO;
import com.wayin.lift.service.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wayin.lift.dao.ClockInDao;
import com.wayin.lift.entity.ClockInEntity;
import com.wayin.lift.service.ClockInService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("clockInService")
public class ClockInServiceImpl extends ServiceImpl<ClockInDao, ClockInEntity> implements ClockInService {

    @Autowired
    private ClockInDao clockInDao;

    @Autowired
    private PersonnelDao personnelDao;

    @Override
    public List<ClockInEntity> findAllClockIn() {
        return clockInDao.selectClockIn();
    }

    @Override
    public List<ClockInVO> findAllVO(Long pId) {
        return clockInDao.selectVO(pId);
    }

    @Override
    // 普通用户即员工打卡
    public int clockIn(Long pId, String temperature) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        String now = df.format(date);
        Date nowTime = df.parse(now);
        int isClock = 0;
        // 判断当天打卡情况
        List<ClockInEntity> clockIns = clockInDao.selectThisDay(pId);
        if (clockIns.size() > 0) {
            for (ClockInEntity c : clockIns) {
                // 判断其打卡数据中打卡类型，若为上班打卡则为1，若为下班打卡则为2，若都已打卡则为3
                switch (isClock){
                    case 0:
                        if (c.getType().equals("0")){
                            isClock = 1;
                        } else if (c.getType().equals("1")){
                            isClock = 2;
                        }
                        break;
                    case 1:
                        if (c.getType().equals("1")){
                            isClock = 3;
                        }
                        break;
                    case 2:
                        if (c.getType().equals("0")){
                            isClock = 3;
                        }
                        break;
                }
            }
        }
        // 进入打卡逻辑
        if (isClock != 3) {
            Date up = df.parse("09:00:00");
            Date down = df.parse("18:30:00");
            Date downStart = df.parse("17:30:00");
            ClockInEntity clock = new ClockInEntity();
            clock.setPId(pId);
            clock.setTemperature(temperature);
            switch (isClock){
                // 当天未打卡，进行上班打卡
                case 0:
//                    if (nowTime.getTime() >= down.getTime()) return 1;
                    if (nowTime.getTime() <= up.getTime()) {
                        clock.setType("0");
                        clock.setState("0");
                        if (clockInDao.insert(clock) > 0) {
                            return 2;
                        }
                    } else if (nowTime.getTime() > up.getTime()) {
                        clock.setType("0");
                        clock.setState("1");
                        if (clockInDao.insert(clock) > 0) {
                            return 2;
                        }
                    }
                    break;
                // 当天以打上班卡，进行下班打卡
                case 1:
                    if (nowTime.getTime() < downStart.getTime()) return 1;
                    if (nowTime.getTime() >= down.getTime()) {
                        clock.setType("1");
                        clock.setState("0");
                        if (clockInDao.insert(clock) > 0) {
                            return 2;
                        }
                    } else if (nowTime.getTime() < down.getTime()) {
                        clock.setType("1");
                        clock.setState("1");
                        if (clockInDao.insert(clock) > 0) {
                            // 返回2为打卡成功
                            return 2;
                        }
                    }
                    break;
            }
            // 返回0为打卡失败
            return 0;
        }
        // 已经打卡，返回1为请勿重复打卡
        return 1;
    }

    @Override
    public Map<String , List<ResultVO>> findSituation(String cDate) {
        List<ResultVO> late = clockInDao.selectLate(cDate);
        List<ResultVO> early = clockInDao.selectEarly(cDate);
        List<ResultVO> onDuty = clockInDao.selectOnDuty(cDate);
        Map<String, List<ResultVO>> map = new HashMap();
        map.put("late", late);
        map.put("early", early);
        map.put("onDuty", onDuty);
        return map;
    }

}